/**
 * Created by zhangrong on 2016/8/6.
 */

/*
新增一个wrap函数，用户封装调用函数
*/
//function proc_msg(func) {
//	function _inn(data) {
//		if(data.status && data.status!='success'){
//			if(data.msg){
//				alert(data.msg)
//			}
//		}else{
//			func(data)
//		}
//	}
//	return _inn
//}

//function has_error(data) {
//	if(data.status && data.status!='success'){
//		if(data.msg){
//			alert(data.msg)
//		}
//		return true
//	}else{
//		return false
//	}
//}



//var org_get=$.get
//$.get=function (url,callback) {
//	var wrap_callback=function (resp) {
//		if(resp.msg){
//			alert(resp.msg)
//		}
//		if(resp.status && resp.status!='success'){
//			return
//		}else{
//			callback(resp)
//		}
//	}
//	org_get(url,wrap_callback)
//}
//var org_post=$.post
//$.post=function (url,data,callback) {
//	var wrap_callback=function (resp) {
//		if(resp.msg){
//			alert(resp.msg)
//		}
//		if(resp.status && resp.status!='success'){
//			return
//		}else{
//			callback(resp)
//		}
//	}
//	org_post(url,data,wrap_callback) 
//}


//function def_proc_port_msg(data,event) {
//	var rt = data.responseJSON
//        if(rt && rt.msg){
//            alert(rt.msg)
//        }
//}

function def_proc_error(jqxhr) {
	if(! window.iclosed){
			if(jqxhr.status !=0){
				alert('server has error, error code is '+jqxhr.status)
			}else{
				alert('maybe server offline,error code is '+jqxhr.status)
			}

		}
}

//window.__proc_port_error=def_proc_port_msg
window.__proc_ajax_error=def_proc_error

export function hook_ajax_msg(proc_port_error,proc_ajax_error){
	if(proc_port_error){window.__proc_port_error=proc_port_error}
	if(proc_ajax_error){window.__proc_ajax_error=proc_ajax_error}
	if(window.hook_ajax_msg_mark){
		return
	}
	window.hook_ajax_msg_mark = true
	$(window).bind('beforeunload',function () {
		window.iclosed=true
	})
	
    //$(document).ajaxSuccess(function (event,data) {
    //    window.__proc_port_error(data,event)
    //})
     $(document).ajaxError(function (event,jqxhr, settings, thrownError) {
		window.__proc_ajax_error(jqxhr)
	})
	//hook_ajax_csrf()
}

export function hook_ajax_csrf() {
		// needed in django context,because django has csrf system enabled by default
	// used for fetch and generate CSRF code for POST ,used with django CSRF middleware
	function getCookie(name) {
	    var cookieValue = null;
	    if (document.cookie && document.cookie !== '') {
	        var cookies = document.cookie.split(';');
	        for (var i = 0; i < cookies.length; i++) {
	            var cookie = jQuery.trim(cookies[i]);
	            // Does this cookie string begin with the name we want?
	            if (cookie.substring(0, name.length + 1) === (name + '=')) {
	                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                break;
	            }
	        }
	    }
	    return cookieValue;
	}
	var csrftoken = getCookie('csrftoken');
	function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
	}
	$.ajaxSetup({
	    beforeSend: function(xhr, settings) {
	        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
	            xhr.setRequestHeader("X-CSRFToken", csrftoken);
	        }
	    }
	});
}



export function show_upload(){
	$('#load_wrap').show()
}
export function hide_upload(second){
	if(second){
		setTimeout(function () {
			$('#load_wrap').hide()
		}, second);
	}else{
		$('#load_wrap').hide()
	}
}

ex.load_css('//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css')
//if(!window.__font_awesome){
//	window.__font_awesome=true
//	document.write(`<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">`)
//}

if(!window.__uploading_mark){
	window.__uploading_mark=true
	document.write(`
		<style>
		.popup{
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			bottom: 0;
			display:none;
			z-index: 9000;
		}
		#_upload_inn{
			background: rgba(88, 88, 88, 0.2);
			border-radius: 5px;
			width:180px;
			height:120px;
			z-index: 9500;
			/*padding:30px 80px ;*/
		}
		.imiddle{
		    position: absolute;
	        top: 50%;
	        left: 50%;
	        transform: translate(-50%, -50%);
	        -ms-transform:translate(-50%, -50%); 	/* IE 9 */
			-moz-transform:translate(-50%, -50%); 	/* Firefox */
			-webkit-transform:translate(-50%, -50%); /* Safari 和 Chrome */
			-o-transform:translate(-50%, -50%); 
			
	        text-align: center;
			/*display: table;*/
	        z-index: 10000;
    	}
    	#_upload_mark{
    		float: left;

    	}
		</style>`)
	$(function(){
		$('body').append(`<div class="popup" id="load_wrap"><div id='_upload_inn' class="imiddle">
		<div  id="_upload_mark" class="imiddle"><i class="fa fa-spinner fa-spin fa-3x"></i></div></div></div>`)
	})
}

